package cn.oesoft.jpa.dao;

import java.util.List;

import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import cn.oesoft.jpa.domain.Discuss;
 
public interface DiscussRepository extends JpaRepository<Discuss, Integer>{
 
    //查询author非空的Discuss评论信息
    public List<Discuss> findByAuthorNotNull();
 
    public List<Discuss> findByAuthorNull();
    //通过文章id分页查询出Discuss评论信息。JPQL
    @Query("SELECT c FROM t_comment c WHERE c.aId = ?1")
    public List<Discuss> getDiscussPaged(Integer aid,Pageable pageable);  
 
    //通过文章id分页查询出Discuss评论信息。原生sql
    @Query(value = "SELECT * FROM t_comment  WHERE  a_Id = ?1",nativeQuery = true)
    public List<Discuss> getDiscussPaged2(Integer aid,Pageable pageable);
 
    //使用命名参数:bb
    @Query("SELECT c FROM t_comment c WHERE c.aId = :bb")
    public List<Discuss> getDiscussPaged3(@Param(value = "bb") Integer aid,Pageable pageable);
 
    //对数据进行更新和删除操作
    @Transactional
    @Modifying
    @Query("UPDATE t_comment c SET c.author = ?1 WHERE c.id = ?2")
    public int updateDiscuss(String author,Integer id);  
 
    @Transactional
    @Modifying
    @Query("DELETE t_comment c WHERE c.id = ?1")
    public int deleteDiscuss(Integer id);
}


